var sites =new Array();
//gis地图
require(["esri/Basemap"], function(Basemap) { 
		
});

require([
     	"esri/Map",
     	"esri/views/MapView",
     	"esri/views/ui/UI", 
     	"esri/layers/FeatureLayer",
     	"esri/Graphic",
     	"esri/widgets/BasemapToggle",
     	"esri/layers/MapImageLayer",
     	"dojo/domReady!"
     	], function(Map, MapView,UI, FeatureLayer,Graphic,BasemapToggle,MapImageLayer){  
     	  var view;
     	  var layer = new MapImageLayer({
     		    url: "http://puhouddns.imwork.net:6080/arcgis/rest/services/行政边界1/行政边界1/MapServer/"
     		  });
     	  var map = new Map({ 
     		  basemap:"dark-gray", 
     		}); 
     	  map.add(layer);
  
  function createView(map) {
       view = new MapView({
        map: map,
        container: "map",
        zoom: 7,  
	    center: [118.78, 32.54] 
      });
       
  }
createView(map);

var basemapToggle = new BasemapToggle({
	  view: view,  // The view that provides access to the map's "streets" basemap
	  nextBasemap: "hybrid"  // Allows for toggling to the "hybrid" basemap
	});
view.ui.add(basemapToggle, "top-right");


function getSiteInfo(target){
	  var result='';
	  $.ajax({
		  url:'../wetlandManager/findbyid',
		  type:'POST',
		  data:{"id":target.graphic.attributes.ID}, 
		  async:false,
		  success:function(res){
				$('#video').html("");
				$('#video').append("<video style='position: relative;z-index: 10;' src="+res.wlin_video[0]+" type='video/mp4'  autoplay width='100%' height='255px' loop>");	
				var data="<p>行政区划: <span class='value'>"+res.wlin_province+res.wlin_city+res.wlin_district+"</span></p>"
			 	+"<p>湿地面积: <span class='value'>"+res.wlin_official_area+"公顷</span></p>"
			 	+"<p>保护体系: <span class='value'>"+res.wlin_protection_level+"</span></p>"
			 	+"<p>管理单位:<span class='value'>"+res.wlin_management_name+"</span></p>";
			 var src="<p><a href='javascript:showinfo(\""+ target.graphic.attributes.ID + "\")'>查看详情</a></p>"
			  result=data+src; 
		  },
		  error:function(err){
			  return err;
		  } 
	  });
	  return result; 
} 
 
//渲染所有监测点
$.ajax({
	url:'../wetlandManager/calculatecenterandscaleForBD',
	type:'POST',
	success:function(res){
		res.forEach(function(item){
			var point=coordtransform.bd09togcj02(item.wlin_longitude,item.wlin_latitude);	
			var points = coordtransform.gcj02towgs84(point[0], point[1]);
			var polyline = {
					 type: "point",  
					 longitude:points[0].toFixed(6),
					 latitude: points[1].toFixed(6)
			  };
	
			// Create an object for storing attributes related to the line
			 var lineAtt = {
			          名字: item.wlin_name,
			          ID: item.id,				    
			  };
					
					
			 var textSymbol ={
			    	  type: "text",  // autocasts as new TextSymbol()
			    	  color: "white",
			    	  haloColor: "black",
			    	  haloSize: "1px",
			    	  text: item.wlin_name,
			    	  xoffset: 8, 
			    	  yoffset: 10,
			    	  font: {  // autocast as new Font()
			    	    size: 10,
			    	    family: "sans-serif",
			    	    weight: "bolder"
			    	  }
			    };	
	 
			 
			 var polylineGraphic1 = new Graphic({
				geometry: polyline,
			    symbol:{
			    	  type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
					  color: '#FF4040',
					  outline: { // autocasts as new SimpleLineSymbol()
					  color: [255, 255, 255],
					  width: 2
					  }		
			    },
			    attributes: lineAtt,
			    popupTemplate: {  // autocasts as new PopupTemplate()
					title: "{名字}",
			/*		content: [{
					   type: "fields",
					   fieldInfos: [{fieldName: "名字"}, {fieldName: "ID"}]
					}]   */
					content:getSiteInfo
				 }
			 });
			 
			
			 
			 var polylineGraphic2 = new Graphic({
					geometry: polyline,
				    symbol: {
				    	  type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
						  color: '#00BFFF',
						  outline: { // autocasts as new SimpleLineSymbol()
						  color: [255, 255, 255],
						  width: 2
						  }		
				    },
				    attributes: lineAtt,
				    popupTemplate: {  // autocasts as new PopupTemplate()
						title: "{名字}",
				/*		content: [{
						   type: "fields",
						   fieldInfos: [{fieldName: "名字"}, {fieldName: "ID"}]
						}]   */
						content:getSiteInfo
					 }
				 });
			 var polylineGraphic3 = new Graphic({
					geometry: polyline,
				    symbol: {
				    	  type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
						  color: '#00FF00',
						  outline: { // autocasts as new SimpleLineSymbol()
						  color: [255, 255, 255],
						  width: 2
						  }		
				    },
				    attributes: lineAtt,
				    popupTemplate: {  // autocasts as new PopupTemplate()
						title: "{名字}",
				/*		content: [{
						   type: "fields",
						   fieldInfos: [{fieldName: "名字"}, {fieldName: "ID"}]
						}]   */
						content:getSiteInfo
					 }
				 });
			 var polylineGraphic4 = new Graphic({
					geometry: polyline,
				    symbol: {
				    	  type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
						  color: '#00EEEE',
						  outline: { // autocasts as new SimpleLineSymbol()
						  color: [255, 255, 255],
						  width: 2
						  }		
				    },
				    attributes: lineAtt,
				    popupTemplate: {  // autocasts as new PopupTemplate()
						title: "{名字}",
				/*		content: [{
						   type: "fields",
						   fieldInfos: [{fieldName: "名字"}, {fieldName: "ID"}]
						}]   */
						content:getSiteInfo
					 }
				 });
			 var polylineGraphic5 = new Graphic({
					geometry: polyline,
				    symbol: {
				    	  type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
						  color: '#B7B7B7',
						  outline: { // autocasts as new SimpleLineSymbol()
							  color: [255, 255, 255],
							  width: 2
						  }
						
				    },
				    attributes: lineAtt,
				    popupTemplate: {  // autocasts as new PopupTemplate()
						title: "{名字}",
				/*		content: [{
						   type: "fields",
						   fieldInfos: [{fieldName: "名字"}, {fieldName: "ID"}]
						}]   */
						content:getSiteInfo
					 }
				 });
			 
			 var polylineGraphics = new Graphic({
					geometry: polyline,
				    symbol:textSymbol,
				    attributes: lineAtt,
				    popupTemplate: {  // autocasts as new PopupTemplate()
						title: "{名字}",
				/*		content: [{
						   type: "fields",
						   fieldInfos: [{fieldName: "名字"}, {fieldName: "ID"}]
						}]   */
						content:getSiteInfo
					 }
				 }); 
			if(item.wlin_protection_level=='国际重要湿地'){
				view.graphics.add(polylineGraphic1);
			}else if(item.wlin_protection_level=='国家重要湿地'){
				view.graphics.add(polylineGraphic2);
			}else if(item.wlin_protection_level=='一般湿地'){
				view.graphics.add(polylineGraphic3);
			}else if(item.wlin_protection_level=='市级重要湿地'){
				view.graphics.add(polylineGraphic4);
			}else if(item.wlin_protection_level=='省级重要湿地'){
				view.graphics.add(polylineGraphic5);
			}else if(item.wlin_protection_level=='其它'){
				view.graphics.add(polylineGraphic5);
			} 	 	 				 			
			// Add the line graphic to the view's GraphicsLayer
			
			view.on("mouse-wheel", function(evt){
				 // deltaY value is postive when wheel is scrolled up
				 // and it is negative when wheel is scrolled down.
				 console.log(view.zoom);
					if(view.zoom>=10){
						view.graphics.add(polylineGraphics);
					}else if(view.zoom<10){
						view.graphics.remove(polylineGraphics);
					}
				}); 
 
		})
		
	},
	error:function(err){
		console.log(err);
	}
	});  
function showbywetlandname(wetlandname){
	//根据湿地公园名称获取视频监测点数据
	getsitesbywetlandname(wetlandname);
	//根据湿地公园名称进行地图展示
	showmapbywetlandname(wetlandname);
}

	$('#select').on('change',function(){
		var wetland=$(this).val();
		var arr=wetland.split('_');
		showbywetlandname(arr[0]);
	});

	
	/**
	 * 根据湿地公园名称获取视频监测点信息
	 */
	function getsitesbywetlandname(wetlandname) {
		$.ajax({
			url : "../monitoringsite/findsiteinfobywetlandname",
			dataType : "json",
			type : "POST",
			data : {'wetlandname':wetlandname},
			async : false,
			error : function(error) {
				console.log(error.responseText);
				$.gritter.add({
					title : '服务器错误',
					text : '对起，服务器错误，请联系系统管理员！',
					class_name : 'gritter-error'
				});
			},
			success : function(strValue) {
				sites.length = 0;
				for ( var key in strValue) {
					if (strValue[key].show == 1){
						sites.push({
							"longitude" : strValue[key].longitude,
							"latitude" : strValue[key].latitude,
							"id" : key,
							"name" : strValue[key].name,
							"status" : strValue[key].status
						});	
					}
				}
			}
		});
	}

	function showmapbywetlandname(wetlandname) {
		var centerlongitude, centerlatitude, zoom;
		$.ajax({
			url : "../wetlandManager/findbyname",
			dataType : "json",
			type : "POST",
			data : {'name':wetlandname},
			async : false,
			error : function(error) {
				console.log(error.responseText);
				$.gritter.add({
					title : '服务器错误',
					text : '对不起，服务器错误，请联系系统管理员！',
					class_name : 'gritter-error'
				});
			},
			success : function(strValue) {
				centerlongitude = strValue.wlin_longitude;
				centerlatitude = strValue.wlin_latitude;
				view.goTo({
					  center: [centerlongitude, centerlatitude],
					  zoom: 10
					}); 
				 var polyline = {
						 type: "point",  
						 longitude: centerlongitude,
						 latitude: centerlatitude
				  };		
				 var textSymbol ={
				    	  type: "text",  // autocasts as new TextSymbol()
				    	  color: "white",
				    	  haloColor: "black",
				    	  haloSize: "1px",
				    	  text: wetlandname,
				    	  xoffset: 8, 
				    	  yoffset: 10,
				    	  font: {  // autocast as new Font()
				    	    size: 10,
				    	    family: "sans-serif",
				    	    weight: "bolder"
				    	  }
				    };	
				 var polylineGraphics = new Graphic({
						geometry: polyline,
					    symbol:textSymbol						  
					 });
				 view.graphics.add(polylineGraphics);
				 
				 view.on("mouse-wheel", function(evt){
						if(view.zoom<=9){
								view.graphics.remove(polylineGraphics);
							}
						}); 

			}
		});
	}
});



// 启动显示
function showinfo(id) {
	window.location.href='../wetlandManager/indexnew?id='+id;
} 
